package com.homni.iomix_xair.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.homni.iomix_xair.R;


/**
 * 
 * 颜色渐变进度条
 * 类说明  音量进度条
 * --------------------------------详细设计：--------------------------------
 * 垂直显示Vertical Model:
 * 颜色保存10种，
 * 区块数量
 * 控件高度
 * 
 * 水平显示Horizontal Model:
 * 
 * bug:宽度小于50dp,过小导致不能显示
 */
public class ColorProgress extends View
{
	//TODO 增加水平显示进度 finish	
	// TODO:当设置不同最大值时，如何确定每个区块的颜色? 	
	
	private String TAG = "ProgressCustom";
	/** 是否垂直显示 */
	private boolean isVertical = true;
	// 进度值图标
	Rect rect;
	/** 矩形宽度 */
	private int marginLeftRight = 10;
	private int marginTop = 10;
	int marginLeftRight0 = 10;
	int marginTopBottom;
	final int RECT_CLOSE_COLOR = 0xff121212;
 
	// 进度最大值,默认30
	private int mMax = 30;
	// 默认显示所有区块
	private int mProgress=30;
	// 画笔绘制矩形
	private Paint paint;
	
	// 矩形块宽
	private int rectSizeX = 10;
	// 矩形块高
	private int rectSizeY = 10;

	// 进度值颜色,根据进度值渐变颜色（10种不同颜色，每种3个）
	int[] rectOpenColors=new int[]{
			//红色	1		
			0xffff5a00,0xffff5a00,0xffff5a00,0xffff5a00,0xffff5a00,
//2
			0xffff7200,0xffff7200,0xffff7200,0xffff7200,0xffff7200,
//3
			0xffff9c00,0xffff9c00,0xffff9c00,0xffff9c00,0xffff9c00,
//4
			0xffffd800,0xffffd800,0xffffd800,0xffffd800,0xffffd800,
//5
			0xffffea00,0xffffea00,0xffffea00,0xffffea00,0xffffea00,
//6
			0xfff0ff00,0xfff0ff00,0xfff0ff00,0xfff0ff00,0xfff0ff00,
//7
			0xffc6ff00,0xffc6ff00,0xffc6ff00,0xffc6ff00,0xffc6ff00,
//8
			0xffa2ff00,0xffa2ff00,0xffa2ff00,0xffa2ff00,0xffa2ff00,
//9
			0xff66ff00,0xff66ff00,0xff66ff00,0xff66ff00,0xff66ff00,
//10			
			//绿色
			0xff24ff00,0xff24ff00,0xff24ff00,0xff24ff00,0xff24ff00
			
			};

	public ColorProgress(Context context)
	{
		super(context);
		//Log.d(TAG, "ProgressCustom(Context context)");
		paint = new Paint();
		paint.setColor(Color.GREEN);
		paint.setStyle(Style.FILL);
	}

	// TOOD:
	public ColorProgress(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		//Log.d(TAG, "ProgressCustom(Context context, AttributeSet attrs)");
		paint = new Paint();
		// paint.setColor(Color.GREEN);
		paint.setStyle(Style.FILL);
		TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ColorProgress);
		isVertical = array.getBoolean(R.styleable.ColorProgress_isVertical, isVertical);
		mMax = array.getInt(R.styleable.ColorProgress_progress_max, mMax);
		mProgress = mMax;
		array.recycle();
	}

	public ColorProgress(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	//	Log.d(TAG, " ProgressCustom(Context context, AttributeSet attrs, int defStyle)");
		// 设置画笔颜色
		paint = new Paint();
		paint.setColor(Color.GREEN);
		paint.setStyle(Style.FILL);
		TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ColorProgress);
		isVertical = array.getBoolean(R.styleable.ColorProgress_isVertical, isVertical);
		mMax = array.getInt(R.styleable.ColorProgress_progress_max, mMax);
		mProgress = mMax;
		array.recycle();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		 height =MeasureSpec.getSize(heightMeasureSpec);
		 width = MeasureSpec.getSize(widthMeasureSpec);
		setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
	}
	// View 高度
	int height;
	// View 宽度
	int width;
	@Override
	protected void onDraw(Canvas canvas)
	{
		// View 高度
		//int height = getHeight();
		// View 宽度
		//int width = getWidth();
		// 计算区块宽
		int rectWith;
		// 计算区块高
		int rectHeight;
		/*********************** Vertical *************************/
		if (isVertical)
		{
			// 设置背景色
			// setBackgroundColor(0xff030303);
			// 总高度分成max等份
			int everyPartHeight = height / mMax;
			/*************** 计算每个区块的位置 ******************/
			// 计算区块宽
			rectWith = width - marginLeftRight0 * 2;
			// 计算区块高
			rectHeight = everyPartHeight >> 1;// everyPartHeight/2
			marginTopBottom = everyPartHeight >> 2;// everyPartHeight/4
			// 矩形x坐标
			// 减去矩形宽度2分之一
			//getPivotX getPivotY围绕View旋转或缩放的x,y的位置
			int xPosition = (int) getPivotX() + width / 2 - (int) rectWith / 2;

			//Log.d(TAG, "mProgress=" + mProgress);
			drawView(mProgress, everyPartHeight, xPosition, rectWith, rectHeight, canvas);

		} else
		{
			/*********************** Horizontal *************************/
			int everyPartWidth = width / mMax;
			marginTopBottom = height >> 2;
			marginLeftRight = everyPartWidth >> 2;
			// 矩形y坐标固定
			int yPositionHorizontal = (int) getPivotY() + marginTopBottom;
			rectWith = everyPartWidth - 2 * marginLeftRight;
			rectHeight = height / 2;
			// 点亮
			for (int i = 1; i <= mProgress; i++)
			{
				//Log.e(TAG, "  draw rect " + i);
				int xPositionHorizontal = (int) getPivotX() + i * everyPartWidth + marginLeftRight;
				paint.setColor(rectOpenColors[Math.abs(rectOpenColors.length/*30*/ - i) % rectOpenColors.length/*30*/]);
				canvas.drawRect(xPositionHorizontal, yPositionHorizontal, xPositionHorizontal + rectWith,
						yPositionHorizontal + rectHeight, paint);
			}
			paint.reset();
		}

		super.onDraw(canvas);
	}

	public void setProgress(int value)
	{
		if (value > mMax)
		{
			Log.e(TAG, "超出最大范围");
			return;
		}
		this.mProgress = value;
		invalidate();
	}

	public int getProgress()
	{
		return mProgress;
	}

	/**
	 * 着色
	 * 
	 * @param mProgress
	 * @param everyPartHeight
	 * @param xPosition
	 * @param rectWith
	 * @param rectHeight
	 * @param canvas
	 */
	public void drawView(int mProgress, int everyPartHeight, int xPosition, int rectWith, int rectHeight, Canvas canvas)
	{
		// 点亮的区块
		for (int i = 1; i <= mProgress; i++)
		{
			//Log.e(TAG, toString() + "  draw rect " + i);
			// 30种颜色轮换
			// paint.setColor(colors[i % 30]);
			// 颜色反序显示
			paint.setColor(rectOpenColors[Math.abs(rectOpenColors.length/*30*/ - i)/* (30-i ) */% /*30*/rectOpenColors.length]);
			int yPosition = (int) getPivotY() + (mMax - i) * everyPartHeight + marginTopBottom;
			canvas.drawRect(xPosition, yPosition, xPosition + rectWith, yPosition + rectHeight, paint);
		}

		// 关闭的区块
		for (int i = 0; i < mMax - mProgress; i++)
		{
		//	Log.e(TAG, "draw rect " + i);
			paint.setColor(RECT_CLOSE_COLOR);
			int yPosition = (int) getPivotY() + i * everyPartHeight + marginTopBottom;
			canvas.drawRect(xPosition, yPosition, xPosition + rectWith, yPosition + rectHeight, paint);
		}
		paint.reset();
	}

	public void drawHorizontal(int mProgress, int everyPartHeight, int xPosition, int rectWith, int rectHeight,
			Canvas canvas)
	{
		// TODO:
	}

	public int getMax()
	{
		return mMax;
	}

	public void setMax(int mMax)
	{
		this.mMax = mMax;
	}

	public void onTouchGetValue()
	{

	}
}
